package com.dajiangtai.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dajiangtai.entity.User;
import com.dajiangtai.service.IUserService;
import com.dajiangtai.util.GlobalConfig;
import com.dajiangtai.util.TimeUtil;

@Controller
@RequestMapping("user")
public class UserController {
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/list")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) throws IOException{		
		ModelAndView mv = new ModelAndView();
		mv.setViewName("user/list");
		return mv;
	}
	
	/**
	 * @function 分页查询问题列表
	 * @param request
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/ulist")
	public void getTableList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("UTF-8");
		List<User> list = null;
		int count = 0;

		String filter = request.getParameter("filter");// 状态过滤
		String sSearch = request.getParameter("sSearch");// 模糊匹配
		int status = Integer.parseInt(filter);
		String sEcho = request.getParameter("sEcho");
		//起始记录
		int iDisplayStart = Integer.parseInt(request
				.getParameter("iDisplayStart"));
		//每页记录数
		int iDisplayLength = Integer.parseInt(request
				.getParameter("iDisplayLength"));
		
		list = userService.getUsers(0, 10);//问题列表
		count = 10;//所有问题记录
		JSONArray jsonDataArray = new JSONArray();
		for (int i = 0; i < list.size(); i++) {
			User user = list.get(i);
			List mylist = new ArrayList();
			mylist.add(user.getUid());
			mylist.add(user.getType());
			mylist.add(user.getStatus());
			jsonDataArray.add(mylist);
		}
		JSONObject returnjobj = new JSONObject();
		returnjobj.put("sEcho", sEcho);
		returnjobj.put("iTotalRecords", count);
		returnjobj.put("iTotalDisplayRecords", count);
		returnjobj.put("aaData", jsonDataArray);
		response.getWriter().write(returnjobj.toString());
	}
	
}

